package com.coding.service.impl;

import com.coding.base.pojo.BaseModel;
import com.coding.base.pojo.PageQueryModel;
import com.coding.base.util.CreateOssUtil;
import com.coding.dao.ClasswithDao;
import com.coding.dao.OssImagesDao;
import com.coding.dao.TheclassDao;
import com.coding.pojo.Classwith;
import com.coding.pojo.OssImages;
import com.coding.pojo.Theclass;
import com.coding.service.TheclassService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 班级信息表
 * (Theclass)表服务实现类
 *
 * @author makejava
 * @since 2020-07-31 14:42:44
 */
@Service
public class TheclassServiceImpl implements TheclassService {
    @Autowired
    TheclassDao theclassDao;
    @Autowired
    OssImagesDao ossImagesDao;
    @Autowired
    ClasswithDao classwithDao;

    @Override
    public void selectAllbypage(PageQueryModel pageQueryModel, BaseModel baseModel) throws Exception {
        PageHelper.startPage(pageQueryModel.getPage(), pageQueryModel.getSize());
        List<Theclass> theclassList = theclassDao.queryAllPage();
        PageInfo pageInfo = new PageInfo(theclassList);
        theclassList = pageInfo.getList();
        if (theclassList.size() == 0) {
            if (pageInfo.getPageNum() > 1) {
                baseModel.setFailure("已经没有下一页了");
            } else {
                baseModel.setOther(2, "没有符合条件的班级");
            }
            return;
        }
        baseModel.setSucceed("查询成功，符合条件的班级共" + theclassList.size() + "个", theclassList);
    }

    @Override
    public void searchClass(int user_id, String text, PageQueryModel pageQueryModel, BaseModel baseModel) throws Exception {
        List<Theclass> theclassList = theclassDao.queryByName(text);
        if (theclassList == null) {
            baseModel.setFailure("查询相关班级失败");
            return;
        }
        for (Theclass theclass : theclassList
        ) {
            Classwith classwith = classwithDao.queryById(user_id, theclass.getClassId());
            if (classwith != null) {
                theclass.setClassMore1("已加入");
            }
        }
        PageHelper.startPage(pageQueryModel.getPage(), pageQueryModel.getSize());
        PageInfo pageInfo = new PageInfo(theclassList);
        theclassList = pageInfo.getList();
        if (theclassList.size() == 0) {
            if (pageInfo.getPageNum() > 1) {
                baseModel.setFailure("已经没有下一页了");
            } else {
                baseModel.setOther(2, "没有符合条件的班级");
            }
            return;
        }
        baseModel.setSucceed("查询成功，符合条件的班级共" + theclassList.size() + "个", theclassList);
    }

    @Override
    public void selectbyclass_id(int class_id, BaseModel baseModel) throws Exception {
        Theclass theclass = theclassDao.queryById(class_id);
        if (theclass != null) {
            baseModel.setSucceed("查询班级信息成功", theclass);
        } else {
            baseModel.setFailure("查询班级信息失败");
        }
    }

    @Override
    public void selectbytag(String tag, PageQueryModel pageQueryModel, BaseModel baseModel) throws Exception {
        PageHelper.startPage(pageQueryModel.getPage(), pageQueryModel.getSize());
        List<Theclass> theclassList = theclassDao.queryByTag(tag);
        PageInfo pageInfo = new PageInfo(theclassList);
        theclassList = pageInfo.getList();
        if (theclassList.size() == 0) {
            if (pageInfo.getPageNum() > 1) {
                baseModel.setFailure("已经没有下一页了");
            } else {
                baseModel.setOther(2, "没有符合条件的班级");
            }
            return;
        }
        baseModel.setSucceed("查询成功，符合条件的班级共" + theclassList.size() + "个", theclassList);
    }

    @Override
    public void insertnewclass(Theclass theclass, MultipartFile file, BaseModel baseModel) throws Exception {
        System.out.println("哈哈哈");
        if (file == null || file.getSize() <= 0) {
            baseModel.setFailure("file不能为空");
            return;
        }

        OssImages ossImages = CreateOssUtil.createOss(file);
        if (ossImages == null || ossImages.getImage_url() == null) {
            baseModel.setFailure("上传图片失败");
            return;
        }
        if (ossImagesDao.insert(ossImages) == 1) {
            theclass.setClassImageId(ossImages.getImage_id());
        } else {
            baseModel.setFailure("上传图片失败");
            return;
        }
        if (theclass.getClassNumbers() == 0) {
            theclass.setClassNumbers(10);
        }
        if (theclass.getClassRank() == 0) {
            theclass.setClassRank(1);
        }
        System.out.println("jjj");
        if (theclassDao.insertSelect(theclass) == 1) {
            System.out.println("hh");
            Theclass theclass1 = theclassDao.queryById(theclass.getClassId());
            if (theclass1 == null) {
                baseModel.setFailure("新增班级失败");
                return;
            }
            baseModel.setSucceed("新增班级成功", theclass1);
        } else {
            baseModel.setFailure("新增班级失败");
        }
    }


    @Override
    public void updateclassintro(Theclass theclass, BaseModel baseModel) throws Exception {
        Theclass theclass1 = theclassDao.queryById(theclass.getClassId());
        if (theclass1 == null) {
            baseModel.setFailure("无法更新，查无此班级");
            return;
        }

        if (theclassDao.update(theclass) == 1) {
            Theclass theclass2 = theclassDao.queryById(theclass.getClassId());
            baseModel.setSucceed("成功更新班级", theclass2);
        } else {
            baseModel.setFailure("更新班级失败");
        }
    }
}